package lib.record;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jdbc.JdbcPort;
import lib.book.bookDao;
import lib.book.bookModule;

public class recordDao {
	
	public boolean borrowBook(String uid, String bno) {
		boolean res=false;
		Connection conn=null;
		PreparedStatement pstmt = null;
		Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String str = simpleDateFormat.format(date);
        System.out.println(str);
        try {
        	bookDao borrow= new bookDao();
        	boolean result=borrow.updateStore(bno, 1);
        	if(result==false) {
        		return res;
        	}
        }catch(Exception e) {
        	e.printStackTrace();
        }
		try {
			conn=JdbcPort.getConnection();
			String sql="insert into libborrow values(?,?,?,?)";
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, uid);
			pstmt.setString(2, bno);
			pstmt.setString(3, str);
			pstmt.setString(4, null);
			int result =pstmt.executeUpdate();
			if(result>0) {
				res=true;
			}
			else {
				bookDao borrow= new bookDao();
	        	borrow.updateStore(bno, 0);
			}
		}catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
		return res;
	}
	
	public boolean returnBook(String uid, String bno) {
		boolean res=false;
		Connection conn=null;
		PreparedStatement pstmt = null;
		ResultSet rs=null;
		Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String str = simpleDateFormat.format(date);
        System.out.println(str);
        try {
        	bookDao borrow= new bookDao();
        	boolean result=borrow.updateStore(bno, 0);
        	if(result==false) {
        		System.out.println("error1");
        		return res;
        	}
        }catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
    		conn=JdbcPort.getConnection();
    		//sql注意为 is null
    		String sql="update libborrow set return_date=? where uid=? and bno=? and return_date is null";
        	pstmt=conn.prepareStatement(sql);
        	pstmt.setString(1, str);
        	pstmt.setString(2, uid);
        	pstmt.setString(3, bno);
        	
        	int result=pstmt.executeUpdate();
        	if(result>0) {
        		res=true;
        	}
        	else {
        		System.out.println("error2");
        		bookDao borrow= new bookDao();
            	borrow.updateStore(bno, 1);
        	}
        }catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
		return res;
	}
	
	public List<recordModule> showRecordByUid(String uid){
		JdbcPort p= new JdbcPort();
		List<recordModule> reall= new ArrayList<recordModule>();
		String sql="SELECT * from libborrow where uid = ?";
		ResultSet rs= p.execQuery(sql, new Object[] {uid});
		try {
			while(rs.next()) {
				recordModule re= new recordModule();
				re.setUid(uid);
				re.setBno(rs.getString("bno"));
				re.setBorrow_date(rs.getString("borrow_date"));
				re.setReturn_date(rs.getString("return_date"));
				reall.add(re);
			}
			return reall;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean dropRecord(String uid) {
		boolean res=false;
		Connection conn=null;
		PreparedStatement pstmt = null;
		try {
			conn=JdbcPort.getConnection();
			String sql="delete from libborrow where uid =?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, uid);
			int result =pstmt.executeUpdate();
			if(result>0) {
				res=true;
			}
		}catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
		return res;
	}
	
//	public static void main(String [] args) {
//		recordDao rd= new recordDao();
//		System.out.println(rd.returnBook("105888", "100001"));
//	}
}
